package com.zh.api.controller.operation;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.FamilyFamilyUserDO;
import com.zh.familyInfo.domain.FamilyUserDTO;
import com.zh.familyInfo.service.FamilyFamilyUserService;
import com.zh.operation.domain.RecipeEatDO;
import com.zh.operation.service.RecipeEatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api("用餐记录")
public class RecipeEatController {
	@Autowired
	private RecipeEatService recipeEatService;
	@Autowired
	private FamilyFamilyUserService familyUserService;


	@Log("所有用餐信息")
	@ResponseBody
	@RequestMapping(value = "listOperationRecipeEat", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationRecipeEat(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<RecipeEatDO> recipeEatList = recipeEatService.list(query);
		for (int i=0;i<recipeEatList.size();i++){
			FamilyFamilyUserDO familyUserDO = familyUserService.get(recipeEatList.get(i).getOlderId());
			if(familyUserDO!=null && familyUserDO.getName()!=null){
				recipeEatList.get(i).setFamilyUserName(familyUserDO.getName());
			}else{
				recipeEatList.get(i).setFamilyUserName("----");
			}
		}
		int total = recipeEatService.count(query);
		PageUtils pageUtils = new PageUtils(recipeEatList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑用餐信息")
	@ResponseBody
	@RequestMapping(value = "editOperationRecipeEatById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationRecipeEatById(@RequestBody RecipeEatDO recipeEat){
		if(recipeEat==null || CheckEmptyUtil.isEmpty(recipeEat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		RecipeEatDO recipeEatDO = recipeEatService.get(recipeEat.getId());
	    return ResultVO.success(recipeEatDO);
	}

	@Log("新增用餐信息")
	@ResponseBody
	@RequestMapping(value = "saveOperationRecipeEat", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationRecipeEat(@RequestBody RecipeEatDO recipeEat){
		if(recipeEat==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(recipeEatService.save(recipeEat)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	@Log("更新用餐信息")
	@ResponseBody
	@RequestMapping(value = "updateOperationRecipeEatById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationRecipeEatById(@RequestBody RecipeEatDO recipeEat){
		if(recipeEat==null || CheckEmptyUtil.isEmpty(recipeEat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		recipeEatService.update(recipeEat);
		return ResultVO.success();
	}

	@Log("删除用餐信息")
	@ResponseBody
	@RequestMapping(value = "removeOperationRecipeEatById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationRecipeEatById(@RequestBody RecipeEatDO recipeEat){
		if(recipeEat==null || CheckEmptyUtil.isEmpty(recipeEat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(recipeEatService.remove(recipeEat.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	@RequestMapping(value = "batchRemoveOperationRecipeEatByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除用餐信息")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(recipeEatService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
}
